package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.dto.RegistrationDto;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjsxt.mapper.RegistrationMapper;
import com.bjsxt.domain.Registration;
import com.bjsxt.service.RegistrationService;
@Service
public class RegistrationServiceImpl implements RegistrationService{

    @Autowired
    private RegistrationMapper registrationMapper;

    /**
     * 添加挂号信息
     * @param registrationDto
     */
    @Override
    public void addRegistration(RegistrationDto registrationDto) {
        Registration registration = new Registration();
        // 复制属性
        BeanUtil.copyProperties(registrationDto,registration);
        // 设置挂号状态为未收费
        registration.setRegistrationStatus(Constants.REG_STATUS_0);
        // 设置创建人
        registration.setCreateBy(registrationDto.getSimpleUser().getUserName());
        // 设置创建时间
        registration.setCreateTime(DateUtil.date());
        // 插入
        registrationMapper.insert(registration);
    }

    /**
     * 根据挂号单id查询挂号单的信息
     * @param regId
     * @return
     */
    @Override
    public Registration queryRegistrationByRegId(String regId) {
        return registrationMapper.selectById(regId);
    }

    /**
     * 修改挂号单的状态为待就诊
     * @param registration
     * @return
     */
    @Override
    public int updateRegistrationById(Registration registration) {
        return registrationMapper.updateById(registration);
    }

    /**
     * 分页查询挂号信息
     * @param registrationDto
     * @return
     */
    @Override
    public DataGridView queryRegistrationForPage(RegistrationDto registrationDto) {
        Page<Registration> page = new Page<>(registrationDto.getPageNum(),registrationDto.getPageSize());
        QueryWrapper<Registration> qw = new QueryWrapper<>();
        // 根据挂号科室进行查询
        qw.eq(null!=registrationDto.getDeptId(),Registration.COL_DEPT_ID,registrationDto.getDeptId());
        // 根据患者名称进行模糊查询
        qw.like(StringUtils.isNotBlank(registrationDto.getPatientName()),Registration.COL_PATIENT_NAME,registrationDto.getPatientName());
        // 根据挂号类型进行查询 门诊、急诊
        qw.eq(StringUtils.isNotBlank(registrationDto.getSchedulingType()),Registration.COL_SCHEDULING_TYPE,registrationDto.getSchedulingType());
        // 根据挂号时间段进行查询
        qw.eq(StringUtils.isNotBlank(registrationDto.getSubsectionType()),Registration.COL_SUBSECTION_TYPE,registrationDto.getSubsectionType());
        // 根据挂号状态进行查询
        qw.eq(StringUtils.isNotBlank(registrationDto.getRegStatus()),Registration.COL_REGISTRATION_STATUS,registrationDto.getRegStatus());
        // 根据就诊日期进行查询
        qw.eq(StringUtils.isNotBlank(registrationDto.getVisitDate()),Registration.COL_VISIT_DATE,registrationDto.getVisitDate());
        // 根据就诊日期进行倒序
        qw.orderByDesc(Registration.COL_VISIT_DATE);
        this.registrationMapper.selectPage(page,qw);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
}
